#include "comment.h"
#include "ui_comment.h"
#include<QMessageBox>
#include <QString>
#include <QtSql>
#include <QSqlDatabase>
#include <QSqlQuery>
extern QString this_user;
Comment::Comment(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Comment)
{
    ui->setupUi(this);
}

Comment::~Comment()
{
    delete ui;
}

void Comment::on_pushButton_clicked()
{
     this->close();
}

void Comment::on_pushButton_2_clicked()
{
    QSqlDatabase data_base = QSqlDatabase::addDatabase("QMYSQL");//添加驱动
    data_base.setHostName("47.110.157.197");  //设置主机地址
    data_base.setPort(3306);  //mysql设置端口
    data_base.setDatabaseName("myproject");  //设置数据库名称
    data_base.setUserName("root");  //设置用户名
    data_base.setPassword("420683ABc@");  //设置密码
    if(!data_base.open())//打开数据库
    {
        qDebug()<<"connect failed";
        qDebug() << data_base.lastError();//.databaseText()  输出错误信息
    }
    else
        qDebug()<<"success";
    QSqlQuery query(data_base);
    QString username=this_user;
    QString this_comment=ui->textEdit->toPlainText();


    /*插入用户注册信息*/
    query.prepare("insert into usr_comment(usr_name,comments)"
                  "values(:username,:user_comment)");
    query.bindValue(":username", username);
    query.bindValue(":user_comment",this_comment);
    bool d=query.exec();

    QMessageBox msgBox;
    if(d){
       msgBox.setText("留言成功.");
       msgBox.exec();
    }
     ui->textEdit->clear();
}

